﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using LinkShareWpf.Commands;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Markup;
using System.Collections.ObjectModel;
using UniCT.LinkShareWpf.Interface;
using System.Collections.Specialized;
using System.Windows.Data;
using UniCT.LinkShareWpf.CommunicationChannel;
using UniCT.LinkShareWpf.Model;


namespace LinkShareWpf.ViewModel
{


    public partial class LocalResourcesViewModel : ViewModelBase
    {
    
        #region Events
        public event EventHandler MessageBoxRequest;
        #endregion

        #region Private
        private Resource resource;
        private Filter filter;
        private ObservableCollection<Resource> filterResources;
        private NameValueCollection bind;
        private string seasonN;
        private string episodeN;
        #endregion
        
        #region Commmands
        public DelegateCommand FilterCommand { get; private set; }
        public DelegateCommand ResetCommand { get; private set; }
        public DelegateCommand ShowNewWindow { get; private set; }
        public DelegateCommand TypeOfResourceCheckedCommand { get; private set; }
        public DelegateCommand RatingOfResourceCheckedCommand { get; private set; }
        public DelegateCommand SelectionChangedCommand { get; private set; }
        #endregion

        #region Properties
        public Resource Resource
        {
            get { return resource; }
            set { resource = value; }
        }
        public ObservableCollection<String> TitleList
        {
            get { return MyResources.Instance.GetTitleList(); }
        }
        public Filter Filter
        {
            get { return filter; }
            set { filter = value; }
        }
        public ObservableCollection<Resource> Resources
        {
            get { return filterResources; }
            set { filterResources = value; }
        }
        public NameValueCollection Bind
        {
            get { return bind; }
            set { bind = value; }
        }
        public string EpisodeN
        {
            get { return episodeN; }
            set { episodeN = value; }
        }
        public string SeasonN
        {
            get { return seasonN; }
            set { seasonN = value; }
        }
        public string[] EpisodeNumbers
        {
            get;
            set;
        }
        #endregion

        public LocalResourcesViewModel()
        {
            filterResources = MyResources.Instance.Resources;
            bind = MyResources.Instance.Bind;
            seasonN = string.Empty;
            episodeN = string.Empty;

            filter = new Filter();
            filter.Type = "allRButton";
            filter.Rating = "allRatRButton";
            FilterCommand = new DelegateCommand(commandParameter => FilterFunction(commandParameter));
            ResetCommand = new DelegateCommand(commandParameter => ResetFunction(commandParameter));
            SelectionChangedCommand = new DelegateCommand(comboBox => SelectionChanged(comboBox));
            TypeOfResourceCheckedCommand = new DelegateCommand(o => TypeOfResource(o));
            RatingOfResourceCheckedCommand = new DelegateCommand(o => RatingOfResource(o));
            WindowHandler.Instance.UpdateResourcesRequest += UpdateResourcesEventHandler;
          
        }

        private void RatingOfResource(object nameOfRadioButton)
        {
            
            filter.Rating = nameOfRadioButton.ToString();
        }
        private void TypeOfResource(object nameOfRadioButton)
        {
            
            filter.Type = nameOfRadioButton.ToString();
        }
        private void UpdateResourcesEventHandler(object sender, EventArgs e)
        {

            filterResources = MyResources.Instance.Resources;

            
            bind = MyResources.Instance.Bind;
            // In caso di aggiunta di una serieTv con un numero di stagione gia esistente
            // Dobbiamo aggiornare il ComboBox relativo al numero di episodio
            EpisodeNumbers = Bind.GetValues(seasonN);


            // Il client grafico da aggiornare in questo caso è proprio il ComboBox delle stagioni
            // e la datagrid
            OnPropertyChanged(null);
          
        }
        private void SelectionChanged(object comboBox)
        {
            if ((comboBox as ComboBox).Name == "seasonCBox")
            {
                EpisodeNumbers = Bind.GetValues(seasonN);
                filter.SeasonN = seasonN; 
                OnPropertyChanged("EpisodeNumbers");
            }
            else
                Filter.EpisodeN = (comboBox as ComboBox).SelectedValue as String;
        }
        public void ShowResource(ObservableCollection<Resource> allResources, Resource res)
        {
          WindowHandler.Instance.ShowWindow(allResources, res, GetType());
        }
        private void ResetFunction(object commandParameter)
        {
            WindowHandler.Instance.ReloadUserControl("LocalUserControl");
        }
        private void FilterFunction(object commandParameter)
        {
            DataGrid dg1 = commandParameter as DataGrid;
            ObservableCollection<Resource> tempRes = filter.FilterResources(MyResources.Instance.Resources);
            if (tempRes.Count == 0)
                MessageBoxRequest(this, new MessageParameterEventArgs("Nessuna corrispondenza."));
            else
                dg1.ItemsSource = tempRes;
        }
        public class MessageParameterEventArgs : EventArgs
        {
            public String message;
            public MessageParameterEventArgs(String msg)
            {
                message = msg;
            }
        }

    }

}
